package common.util.string;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class StringUtil {

	/**
	 * 把传入字符串首字母转成大写字母
	 * 
	 * @param str
	 * @return result
	 */
	public static String ucfirst(String str) {
		String result = str;
		if (str.length() > 0) {
			char firstCharacter = str.charAt(0);
			if (Character.isLetter(firstCharacter)
					&& Character.isLowerCase(firstCharacter)) {
				result = Character.toUpperCase(firstCharacter)
						+ str.substring(1, str.length());
			}
		}
		return result;
	}

	/**
	 * 把传入字符串首字母转换成小写字母
	 * 
	 * @param str
	 * @return
	 */
	public static String lcfirst(String str) {
		String result = str;
		if (str.length() > 0) {
			char firstCharacter = str.charAt(0);
			if (Character.isLetter(firstCharacter)
					&& Character.isUpperCase(firstCharacter)) {
				result = Character.toLowerCase(firstCharacter)
						+ str.substring(1, str.length());
			}
		}
		return result;
	}

	/**
	 * MD5 加密
	 */
	public static String md5(String str) {
		MessageDigest messageDigest = null;

		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
			System.exit(-1);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}

		return md5StrBuff.toString();
	}

	/**
	 * 判定给定字符串是否为空 判定为空的条件： 1. 该字符串为null 2. 该字串isEmpty判定为true
	 * 
	 * @param data
	 * @return
	 */
	public static boolean isEmpty(String data) {
		boolean bRtn = false;
		if ( data == null || data.equals("") || data.isEmpty() ) {
			bRtn = true;
		}
		return bRtn;
	}

	/**
	 * 清除html标记
	 * 
	 * @param html
	 * @return
	 */
	public static String stripHtmlTags(String html) {
		if (!isEmpty(html)) {
			html = html.replaceAll("\\&[a-zA-Z]{1,10};", "")
					.replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");
		}
		return html;
	}

	/**
	 * 清除特殊字符
	 * 
	 * @param str
	 * @return
	 */
	public static String stripSpecialChar(String str) 
			throws PatternSyntaxException {
		if ( !isEmpty(str) ) {
			String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(str);
			str = m.replaceAll("").trim();
		}
		return str;
	}
	
	/**
	 * 截取指定长度字符串
	 * 
	 * @param str
	 * @param limitLength
	 * @return
	 */
	public static String cutLimitLengthStr(String str, int limitLength) {
		if ( !isEmpty(str) && (str.length() > limitLength) ) {
			str = str.substring(0, limitLength) + " ...";
		}
		return str;
	}

	public static void main(String[] args) {
		// String str = "";
		// System.out.println(ucfirst(str));
		// System.out.println(lcfirst(str));
	}
}
